<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;

/**
 * Class Building.
 *
 * @package namespace App\Models;
 */
class Building extends BaseModel implements Transformable
{
    use TransformableTrait;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['company_id', 'campus_id', 'building_type_id', 'building_nock', 'dorm_type_id', 'name', 'short_name', 'unit_name_type', 'unit_count', 'floors', 'rooms_per_floor', 'beds_per_room', 'general_room_rent', 'record_order', 'gen_dorm_rule', 'remark'];

    public function dorms()
    {
        return $this->hasMany(Dorm::class)->orderBy('record_order')->orderBy('code');
    }

    public function dormType()
    {
        return $this->belongsTo(DormType::class);
    }

    public function scopeShouldInstallMeter(Builder $builder)
    {
        return $builder->whereHas('electricFeeType', function ($q) {
            $q->where('compute_unit', '元/计量单位');
        });
    }

    protected static function boot()
    {
        parent::boot();
        static::creating(function ($model) {
            if (auth()->check()) {
                $model->company_id = auth()->user()->company_id ?? null;
            }
        });
    }
}
